<?php

namespace app\controllers\admin;

use app\controllers\common\BaseController;
use app\service\UrlService;
use yii;
use yii\web\Controller;
use app\models\Cheoice;
use app\models\Judges;
use app\models\Radio;
use app\models\WordQuest;
use app\models\Completion;
use app\models\KsSurvey;


class PaperController extends BaseController
{

	public function actionIndex()
	{

		$id = $this->current_user['id'];
		return $this->render('index',[
			"tid"=> $id,
		]);
	}
	public function actionAddpaper(){
		$data = $this->post(null);
		try{
			$radio_title = $data['radio_title'];
			$radio_q = $data['radio_q'];
			$radio_ans = $data['radio_ans'];
		
		//
			$box_title = $data['box_title'];
			$box_q = $data['box_q'];
			$box_ans = $data['box_ans'];
		
		//
			$judge_title = $data['judge_title'];
			
			$judge_ans = $data['judge_ans'];
			//
			$completion = $data['completions'];
			//
			$boss = $data['shotanswer'];
			//试卷概况
			$survey = $data['survey'];
		}catch(\Exception $e){
		    echo "每种题型最少一道,请点击回退按钮，返回上一页";
			return $e->getMessage();
		}
		// 出卷人id
		$tid = $data['tid'];

		$radio = [];
		$checkbox = [];
		$judge = [];
		$completionq = [];
		$bossq = [];
		$nowtime = date('Y-m-d H:i:s');
		foreach ($radio_title as $key => $value) {
			$radiocurr = [
				'title'=>$value,
				'options'=>json_encode($radio_q[$key]),
				'answer'=>$radio_ans[$key],
			];
			$radio[] = $radiocurr;
		$Radio_model = new Radio();
		$Radio_model->setAttributes($radiocurr);
		$Radio_model->tid = $tid;
		$Radio_model->save();
		}
		foreach ($box_title as $key => $value) {
			$checkboxcurr = [
				'title'=>$value,
				'options'=>json_encode($box_q[$key]),
				'answer'=>$box_ans[$key],
			];
			$checkbox[] = $checkboxcurr;
            $Cheoice_model = new Cheoice();
            $Cheoice_model->setAttributes($checkboxcurr);
            $Cheoice_model->tid = $tid;
            $Cheoice_model->save();
		}

		foreach ($judge_title as $key => $value) {
			$judgecurr = [
				'title'=>$value,
				'is_true'=>$judge_ans[$key],
			];
			$judge[] = $judgecurr;
		$Judge_model = new Judges();
		$Judge_model->setAttributes($judgecurr);
		$Judge_model->tid = $tid;
		$Judge_model->save();
		}
		foreach ($completion as $key => $value) {
			$compoletionqcurr = [
				'title'=>$value,

			];
			$completionq[] = $compoletionqcurr;
		$Completion_model = new Completion();
		$Completion_model->setAttributes($compoletionqcurr);
		$Completion_model->tid = $tid;
		$Completion_model->save();
		}

		foreach ($boss as $key => $value) {
			$bossqcurr = [
				'title'=>$value,
			];
			$bossq[] = $bossqcurr;
		$WordQuest_model = new WordQuest();
		$WordQuest_model->setAttributes($bossqcurr);
		$WordQuest_model->tid = $tid;
		$WordQuest_model->create_time = $nowtime;
		$WordQuest_model->save();
		}
		$papercontent = [
			'radio' => $radio,
			'checkbox'=>$checkbox,
			'completion'=>$completionq,
			'judge'=>$judge,
			'boss'=>$bossq,
		];

		$KsSurvey_model = new KsSurvey();
		$KsSurvey_model->setAttributes($survey);
		$KsSurvey_model->fullmarks = $data['fullmarks'];
		$KsSurvey_model->tid = $tid;
		$KsSurvey_model->exampinfo = json_encode($papercontent);
		$KsSurvey_model->save();
		return $this->render(UrlService::BuildUrl('/error/error'),['message'=>'添加成功']);
		
	}
	public function actionRedis(){
		$redis = new \Redis();  
		$redis->connect('127.0.0.1', 6379);//serverip port
		
		// $redis ->set( "test" , "Hello World");  
		echo $redis ->zcard( "zset");
	}
}